package edu.hitsz.rankinglist;
import java.util.LinkedList;

public class RankDaoImpl implements RankDao {

    private LinkedList<Rank> ranks;

    public RankDaoImpl(LinkedList<Rank> ranks){
        this.ranks = ranks;
    }

    @Override
    public LinkedList<Rank> getAllRank() {
        return ranks;
    }

    @Override
    public void addRank(Rank rank) {
        int curScore = rank.getScore();
        int flag = 0;

        /** 按分数高低维护ranks */
        for(int i=0;i<=ranks.size()-1;++i){
            if(curScore > ranks.get(i).getScore()){
                ranks.add(i,rank);
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            ranks.addLast(rank);
        }
    }

    public void deleteRank(int index){
        ranks.remove(index);
    }

    @Override
    public void printRankingList() {
        System.out.println("*************************************************");
        System.out.println("                  得分排行榜                       ");
        System.out.println("*************************************************");
        for(int i=0;i<=ranks.size()-1;++i){
            System.out.printf("第%d名: %s, %d, %s\n",i+1,ranks.get(i).getUserName(),ranks.get(i).getScore(),ranks.get(i).getTime());
        }
    }
}
